/**
 * 
 */
package front_shop;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.JTableHeader;

/**
 * @author wx
 *
 */
public class return_sales extends JFrame implements ActionListener {

	/**
	 * @param args
	 */
	
	JPanel panel;
	JLabel bh;
	JTextField spbh;
	JTable table;
	JButton delete,ok;
	Object t[][]= new Object[1][2];
	Connection con;
    Statement sql; 
    ResultSet rs;
	int i=0;
	
	public return_sales(){
		panel=new JPanel();
		bh=new JLabel("商品编号");
		spbh=new JTextField(20);
		delete=new JButton("删除");
		ok=new JButton("提交");
		
		panel.setLayout(null);
		this.add(panel);
		panel.add(bh);
		bh.setBounds(20,20,80,20);
		panel.add(spbh);
		spbh.setBounds(80,20,80,20);

		panel.add(delete);
		delete.setBounds(120,100,80,20);
		panel.add(ok);
		ok.setBounds(300,100,80,20);
		
		Object columnname[]={"商品编号","商品名称"};
		table=new JTable(t,columnname);
		JTableHeader tableheader=table.getTableHeader();
		panel.add(tableheader);
		panel.add(table);
		tableheader.setBounds(20,50,460,20);
		table.setBounds(20,70,460,18);
		
		spbh.addActionListener(this);
//		dj.addActionListener(this);
		ok.addActionListener(this);
		delete.addActionListener(this);
		
		try{  
		    Class.forName("com.mysql.jdbc.Driver");
		    }
	 	catch(Exception e){
		       System.out.println("忘记把MySQL数据库的JDBC-数据库驱动程序复制到JDK的扩展目录中");
		    }
	 	try { String uri= "jdbc:mysql://127.0.0.1:3306/javashop";
		          String user="root";
		          String ps="123";
		          con=DriverManager.getConnection(uri,user,ps);
		          sql=con.createStatement();
		    }
	    catch(SQLException e){ 
		          System.out.println(e);
		    }
		
		
		
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		return_sales user=new return_sales();
		user.setTitle("退货 ");
		user.setBounds(200,200,500,170);
		user.setVisible(true);
		user.validate();
		user.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource()==spbh){
			try{	
				if (spbh.getText().length()==7){
				
						rs=sql.executeQuery("SELECT * FROM merchandise where MerchandiseNumber=\""+spbh.getText()+"\"");
						 if(rs.next()==true){
							 t[0][0]=rs.getString(2);
							 t[0][1]=rs.getString(5);
							 table.repaint();
						 }
						 else{
							 JOptionPane.showMessageDialog(this,"编号输入有误！！！","退货",JOptionPane.WARNING_MESSAGE);
							 spbh.setText("");
							 spbh.requestFocus();
						 }
				}//legnth
				
			}
			catch(SQLException c){ 
		          System.out.println(c);
		    }
				
			
			
		}//spbh
	
		else if(e.getSource()==ok){
			double dj;
			try{	
				if (spbh.getText().length()==7){
					if(spbh.getText().startsWith("SC")||spbh.getText().startsWith("SX")){
						 JOptionPane.showMessageDialog(this,"蔬菜生鲜一经售出，概不退还！！！","退货",JOptionPane.WARNING_MESSAGE);
						 spbh.setText("");
						 spbh.requestFocus();
						 t[0][0]="";
						 t[0][1]="";
						 table.repaint();
	
					}
					else{
						rs=sql.executeQuery("SELECT * FROM merchandise where MerchandiseNumber=\""+spbh.getText()+"\"");
						 if(rs.next()==true){
							 t[0][0]=rs.getString(2);
							 t[0][1]=rs.getString(5);
							 dj=Double.parseDouble(rs.getString(6));
							 table.repaint();
							 rs=sql.executeQuery("SELECT * FROM stock where MerchandiseNumber=\""+spbh.getText()+"\"");
							 if(rs.next()==true){
								int amount=Integer.parseInt(rs.getString(3))+1;
								String w="update stock set number="+amount+" where MerchandiseNumber=\""+spbh.getText()+"\"";
								int ok=sql.executeUpdate(w);
							 }
							 else {//在库存表中插入记录
								 String w="insert into stock values(\""+spbh.getText().substring(2)+"\",\""+spbh.getText()+"\",1)";
									int ok=sql.executeUpdate(w);
							 }
							 JOptionPane.showMessageDialog(this,"退货成功","退货",JOptionPane.WARNING_MESSAGE);
							 							 
							 Date nowTime=new Date();
							 String time=String.format("%tY-%<tm-%<td", nowTime);
							 rs=sql.executeQuery("SELECT * FROM cashbalance where date=\""+time+"\"");
							 if(rs.next()==true){//更新
								 dj=Double.parseDouble(rs.getString(3))+dj;
								 String w="update cashbalance set expend="+dj+" where date=\""+time+"\"";
								 int ok=sql.executeUpdate(w);
							 }
							 else
							 {
								String w="insert into cashbalance (date,expend) values ( \""+time+"\","+dj+")";
								int  ok=sql.executeUpdate(w);
							 }
							 
							 
								rs=sql.executeQuery("SELECT * FROM refund where date=\""+time+"\" and MerchandiseNumber=\""+t[0][0].toString()+"\" ");
								if(rs.next()==true){//更新
									Double x=Double.parseDouble(rs.getString(4))+1;
									System.out.println(x);

									String  w="update refund set amount="+x+" where date=\""+time+"\"and MerchandiseNumber=\""+t[0][0].toString()+"\" ";
									int  ok=sql.executeUpdate(w);
								}
								else{
									String  w="insert into refund  values ( \""+time+"\",\""+t[0][0].toString()+"\",\""+t[0][1].toString()+"\","+1+")";
									int  ok=sql.executeUpdate(w);

								}
								 
							 
							 t[0][0]="";
							 t[0][1]="";
							 table.repaint();
							 spbh.setText("");
							 spbh.requestFocus();	
						 }
						 else{
							 JOptionPane.showMessageDialog(this,"编号输入有误！！！","退货",JOptionPane.WARNING_MESSAGE);
							 spbh.setText("");
							 spbh.requestFocus();
						 }
					}
				}//legnth
				
			}
			catch(SQLException c){ 
		          System.out.println(c);
		    }
				
		}
		if(e.getSource()==delete){
			 
			 t[0][0]="";
			 t[0][1]="";
			 table.repaint();
			 spbh.setText("");
			 spbh.requestFocus();		
			 }
	}
}
